package utils;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.ImmutableTriple;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: DRevelation
 * @Date: 2021/7/19 10:33
 * 常用工具方法测试
 */
public class CommonUtils {
    public static void main(String[] args) throws ParseException {
        // jdk
        // 1.1 如何把list集合拼接成以逗号分隔的字符串 a,b,c
        List<String> list = Arrays.asList("a", "b", "c");
        String collect = list.stream().collect(Collectors.joining(","));
        System.out.println("collect = " + collect);
        String join = String.join(",", list);
        System.out.println("join = " + join);
        // 1.3 比较两个对象是否相等（避免空指针）
        boolean equals = Objects.equals("a", "b");

        // apache commons
        // 2.1.4 格式化日期
        String format = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        System.out.println("format = " + format);
        Date date = DateUtils.parseDate("2021-07-19 01:01:01", "yyyy-MM-dd HH:mm:ss");
        System.out.println("date = " + date);
        // 2.1.5 包装临时对象
        ImmutablePair<String, Integer> pair = ImmutablePair.of("a", 1);
        System.out.println(pair.getLeft() + ": " + pair.getRight());
        ImmutableTriple<Integer, String, Date> triple = ImmutableTriple.of(2, "b", new Date());
        System.out.println("triple = " + triple);
        // guava
    }
}
